import httpInstance from "@/utils/http.js";

/**
 * 分页条件查询控制点
 * @param {number} current - 当前页码
 * @param {number} pageSize - 每页显示数量
 * @param {Object} params - 查询条件对象
 * @returns {Promise<Object>} 返回包含控制点列表的Promise对象
 * @throws {Error} 当参数无效或请求失败时抛出错误
 */
export function getControlPoints(current, pageSize, params) {
    if (!current || !pageSize) {
        throw new Error('页码和每页数量不能为空');
    }
    
    const queryParams = new URLSearchParams({
        current: current,
        pageSize: pageSize,
        ...params
    });

    return httpInstance({
        url: `/controlPoints/page?${queryParams.toString()}`,
        method: "get"
    });
}

/**
 * 根据ID查询控制点详情
 * @param {string|number} id - 控制点ID
 * @returns {Promise<Object>} 返回包含控制点详情的Promise对象
 * @throws {Error} 当ID无效或请求失败时抛出错误
 */
export function getControlPointById(id) {
    if (!id) {
        throw new Error('ID不能为空');
    }
    return httpInstance({
        url: `/controlPoints/${id}`,
        method: "GET",
    });
}

/**
 * 添加新控制点
 * @param {Object} controlPoint - 控制点数据对象
 * @returns {Promise<Object>} 返回包含新增结果的Promise对象
 * @throws {Error} 当参数无效或请求失败时抛出错误
 */
export function addControlPointApi(controlPoint) {
    if (!controlPoint) {
        throw new Error('控制点数据不能为空');
    }
    return httpInstance({
        url: `/controlPoints`,
        method: "POST",
        data: controlPoint
    });
}

/**
 * 更新控制点信息
 * @param {Object} controlPoint - 包含ID和控制点数据的对象
 * @returns {Promise<Object>} 返回包含更新结果的Promise对象
 * @throws {Error} 当参数无效或请求失败时抛出错误
 */
export function updateControlPointById(controlPoint) {
    if (!controlPoint?.id) {
        throw new Error('控制点ID不能为空');
    }
    return httpInstance({
        url: `/controlPoints/${controlPoint.id}`,
        method: "put",
        data: controlPoint
    });
}

/**
 * 根据ID删除控制点
 * @param {string|number} id - 控制点ID
 * @returns {Promise<Object>} 返回包含删除结果的Promise对象
 * @throws {Error} 当ID无效或请求失败时抛出错误
 */
export function delControlPointById(id) {
    if (!id) {
        throw new Error('ID不能为空');
    }
    return httpInstance({
        url: `/controlPoints/${id}`,
        method: "DELETE",
    });
}
